<?php
require_once('libs/BomCommon.php');

function doHellow($input) {	
	$log = new Logger();
	$log->log("BOMBERMAN TEST");
	
    return "Test - เทสข้อมูล ".$input;
}
function newBPValue($pid, $bphigh, $bplow){
	$log = new Logger();
	$log->log("Process new BP Value");
	$log->log("\$pid=".$pid);
	$log->log("\$bphigh=".$bphigh);
	$log->log("\$bplow=".$bplow);
	return "SUCCESS";
}
function testResObject($input){
	return /*array(
        'Name' => array('name'=>'name','type'=>'xsd:string'),
        'Code' => array('name'=>'product_number','type'=>'xsd:string'),
		'Price' => array('name'=>'price','type'=>'xsd:decimal'),
        'Ammount' => array('name'=>'quantity','type'=>'xsd:int')
    );*/ array(
        'Name' => 'BOM',
        'Code' => 'AON002',
		'Price' => 0.1,
        'Ammount' => 1
    );
}
function testResObjectArray($input){
	return array(
		array(
			'Name' => 'BOM1',
			'Code' => 'AON001',
			'Price' => 0.1,
			'Ammount' => 1
		),
		array(
			'Name' => 'บอม',
			'Code' => 'AON002',
			'Price' => 0.2,
			'Ammount' => 2
		),
		array(
			'Name' => 'BOM3',
			'Code' => 'AON003',
			'Price' => 0.3,
			'Ammount' => 3
		)
	);
}
function getSysDateTime($private_key){
	$log = new Logger();
	$log->log("==saveUserData(private_key=".$private_key.", userid=".$userid.", systolic=".$systolic.", diastolic=".$diastolic.", pulse=".$pulse.")==");
	$sys_date = "1/1/1900";
	$sys_time = "00:00";
	if( (BomCommon::checkPrivateKey($private_key)) ){
		$sys_date = date("j/n/Y");
		$sys_time = date("G:i");
	}
	$sysDateTime = array('SysDate' => $sys_date, 'SysTime' => $sys_time);
	$log->log("sysDateTime=".print_r($sysDateTime, true));
	return $sysDateTime;
}
function searchUserList($private_key, $input){
	$log = new Logger();
	$arrUser = array();
	$input = trim($input);
	$log->log("==searchUserList()==");
	$log->log("private_key->".$private_key);
	$log->log("input->".$input);
	if( (BomCommon::checkPrivateKey($private_key)) && !(empty($input)) ){
		$log->log("begin do searchUserList()");
		$link = BomCommon::openDBConnection();
		$strSQL = "SELECT u.user_id as id, u.user_fname as fname, u.user_lname as lname, ";
		$strSQL.= " DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(u.user_dateofbirth)), '%Y')+0 as age ";
		$strSQL.= " FROM clum_users u ";
		$strSQL.= " WHERE user_fname LIKE '%".$input."%' OR user_lname LIKE '%".$input."%'";
		$data = BomCommon::mysql_ToArray(BomCommon::excuteDB($link, $strSQL));
		$log->log($strSQL);
		for($i=0; $i<count($data); $i++){
			array_push($arrUser, array(
				'Userid' => $data[$i]['id'],
				'Fname' => $data[$i]['fname'],
				'Lname' => $data[$i]['lname'],
				'Age' => $data[$i]['age'],
				)
			);
		}
	}
	$log->log("size of data->".count($arrUser));
	$log->log("end searchUserList()");
	return $arrUser;
}
function getBPHistory($private_key, $input){
	$log = new Logger();
	$arrBPHistory = array();
	$input = trim($input);
	$log->log("==searchUserList()==");
	$log->log("private_key->".$private_key);
	$log->log("input->".$input);
	if( (BomCommon::checkPrivateKey($private_key)) && !(empty($input)) ){
		$log->log("begin do searchUserList()");
		$link = BomCommon::openDBConnection();
		$strSQL = "select bp.presure_mech_id, bp.user_id, round(bp.sys) as sys, round(bp.dia) as dia, 
						round(bp.hr) as hr, bp.add_date/*date_format(bp.add_date, '%e/%l/%Y') as add_date
						, date_format(bp.add_date, '%H:%i') as add_time*/
					from clum_user_presure_mech bp 
					where bp.user_id=".$input."
					order by bp.add_date desc";
		$data = BomCommon::mysql_ToArray(BomCommon::excuteDB($link, $strSQL));
		$log->log($strSQL);
		for($i=0; $i<count($data); $i++){
			$stryear = date("Y",strtotime($data[$i]['add_date']));
			$strmonth = date("m",strtotime($data[$i]['add_date']));
            $strday = date("d",strtotime($data[$i]['add_date']));
			$strhour = date("H",strtotime($data[$i]['add_date']));
            $strminute = date("i",strtotime($data[$i]['add_date']));

			array_push($arrBPHistory, array(
				'BPid' => $data[$i]['presure_mech_id'],
				'BPDate' => $strday."/".$strmonth."/".$stryear ,
				'BPTime' => $strhour.":".$strminute,
				'BPSyst' => $data[$i]['sys'],
				'BPDias' => $data[$i]['dia'],
				'BPPulse' => $data[$i]['hr']
				)
			);
		}
	}
	$log->log("size of data->".count($arrBPHistory));
	$log->log("end searchUserList()");
	return $arrBPHistory;
}
function saveUserData($private_key, $userid, $systolic, $diastolic, $pulse){
	$log = new Logger();
	$status_code = "0";
	$status_desc = "";
	$log->log("==saveUserData(private_key=".$private_key.", userid=".$userid.", systolic=".$systolic.", diastolic=".$diastolic.", pulse=".$pulse.")==");
	if( (BomCommon::checkPrivateKey($private_key)) && !(empty($userid)) && !(empty($systolic)) && !(empty($diastolic)) && !(empty($pulse)) ){
		$link = BomCommon::openDBConnection();
		$strSQL = "insert into clum_user_presure_mech (presure_mech_id, user_id, sys, dia, hr, add_date)
					values (null, ".$userid.", ".$systolic.", ".$diastolic.", ".$pulse.", now())";
		if(BomCommon::excuteDB($link, $strSQL)){
			$status_code = "1";
			$status_desc = "Save complete.";
		}else{
			$status_code = "2";
			$status_desc = "Execute query failed -> ".mysql_error();
		}
	}else{
		$status_code = "5";
		$status_desc = "Private key wrong, or some data not found.";
	}
	$status = array('Code' => $status_code, 'Desc' => $status_desc);
	$log->log("status=".print_r($status, true));
	return $status;
}
?>